﻿using System;
using System.Runtime.InteropServices;

namespace Dynamic.Core.Utility
{
    /// <summary>
    /// 系统时间类型
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    public struct SystemTime
    {
        /// <summary>
        /// 年
        /// </summary>
        public ushort year;

        /// <summary>
        /// 月
        /// </summary>
        public ushort month;

        /// <summary>
        /// 周
        /// </summary>
        public ushort dayofweek;

        /// <summary>
        /// 天
        /// </summary>
        public ushort day;

        /// <summary>
        /// 小时
        /// </summary>
        public ushort hour;

        /// <summary>
        /// 分
        /// </summary>
        public ushort minute;

        /// <summary>
        /// 秒
        /// </summary>
        public ushort second;

        /// <summary>
        /// 毫秒
        /// </summary>
        public ushort milliseconds;
    }


    /// <summary>
    /// 调用系统API
    /// </summary>
    internal class WinTimeAPI
    {
        [DllImport("Kernel32.dll")]
        public static extern bool SetSystemTime(ref SystemTime sysTime);
        [DllImport("Kernel32.dll")]
        public static extern bool SetLocalTime(ref SystemTime sysTime);
        [DllImport("Kernel32.dll")]
        public static extern void GetSystemTime(ref SystemTime sysTime);
        [DllImport("Kernel32.dll")]
        public static extern void GetLocalTime(ref SystemTime sysTime);

    }

    /// <summary>
    /// 系统时间通用类
    /// </summary>
    public class SystemTimeUtility
    {
        /// <summary>
        /// 设置系统时间
        /// </summary>
        /// <param name="newdatetime">新时间</param>
        /// <returns></returns>
        public static bool SetSysTime(DateTime newdatetime)
        {
            SystemTime st = ConvertDateTimeTonSystemTime(newdatetime);
            return WinTimeAPI.SetLocalTime(ref st);
        }

        /// <summary>
        /// 转化成系统时间格式
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static SystemTime ConvertDateTimeTonSystemTime(DateTime dt)
        {
            SystemTime st = new SystemTime();
            st.year = Convert.ToUInt16(dt.Year);
            st.month = Convert.ToUInt16(dt.Month);
            st.day = Convert.ToUInt16(dt.Day);
            st.dayofweek = Convert.ToUInt16(dt.DayOfWeek);
            st.hour = Convert.ToUInt16(dt.Hour);
            st.minute = Convert.ToUInt16(dt.Minute);
            st.second = Convert.ToUInt16(dt.Second);
            st.milliseconds = Convert.ToUInt16(dt.Millisecond);

            return st;
        }

        /// <summary>
        /// 转化成Datetime
        /// </summary>
        /// <param name="st"></param>
        /// <returns></returns>
        public static DateTime ConvertSystemTimeToDateTime(SystemTime st)
        {
            return new DateTime(st.year, st.month, st.day, st.hour, st.minute, st.second, st.milliseconds);
        }
    }
}
